import type { ISearchListData } from '@/components/search/types'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { removeDuplicate } from '@/components/search/components/headerSearch'

export interface IData {
    historySearch: ISearchListData[]
    collectSearch: ISearchListData[]
}

export const headerSearchStore = defineStore('historySearch', {
    state: () => {
        const data = ref<IData>({
            historySearch: [],
            collectSearch: [],
        })
        return {
            data,
        }
    },
    getters: {},
    actions: {
        setHistorySearch(state: any) {
            this.data.historySearch.unshift(state)
            const unique = removeDuplicate(this.data.historySearch)
            for (let i = 0; i < unique.length; i++) {
                unique[i].isActive = false
            }
            this.data.historySearch = unique
        },
        setCollectSearch(state: any) {
            this.data.collectSearch.unshift(state)
            const unique = removeDuplicate(this.data.collectSearch)
            for (let i = 0; i < unique.length; i++) {
                unique[i].isActive = false
            }
            this.data.collectSearch = unique
        },
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'headerSearch', //自定义 Key值
                storage: localStorage, // 选择存储方式
            },
        ],
    },
})
